package cn.dgut.training.dto.goods;

import cn.dgut.training.dto.BaseDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class GoodsListDTO extends BaseDTO {

    /**
     * 商品编号
     */
    private Long id;

    private String goodsSn;

    private Long categoryId;

    private String name;

    private String isbn;

    private String author;

    private String keywords;


    //扩展分类查询
    public static QueryWrapper getWrapper(GoodsListDTO goodsListDTO){

        QueryWrapper queryWrapper=new QueryWrapper();

        if(goodsListDTO.getId()!=null){
            queryWrapper.eq("id",goodsListDTO.getId());
        }
        if(goodsListDTO.getCategoryId()!=null){
            queryWrapper.eq("category_id",goodsListDTO.getCategoryId());
        }
        if(!StringUtils.isBlank(goodsListDTO.getGoodsSn())){
            queryWrapper.like("goods_sn",goodsListDTO.getGoodsSn());
        }
        if(!StringUtils.isBlank(goodsListDTO.getName())){
            queryWrapper.like("name",goodsListDTO.getName());
        }
        if(!StringUtils.isBlank(goodsListDTO.getIsbn())){
            queryWrapper.like("isbn",goodsListDTO.getIsbn());
        }
        if(!StringUtils.isBlank(goodsListDTO.getAuthor())){
            queryWrapper.like("author",goodsListDTO.getAuthor());
        }
        if(!StringUtils.isBlank(goodsListDTO.getKeywords())){
            queryWrapper.like("keywords",goodsListDTO.getKeywords());
        }

        if(!StringUtils.isBlank(goodsListDTO.getOrderBy())){
            if("desc".equals(goodsListDTO.getSort())){
                queryWrapper.orderByDesc(goodsListDTO.getOrderBy());
            }else {
                queryWrapper.orderByAsc(goodsListDTO.getOrderBy());
            }

        }
        return queryWrapper;
    }
}
